<?php

class FriendModel extends Model {

    public static function friends($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return array('ecode' => ERR302);
        
        $ret = daocall("friend", "getfriends", $uid);
        if ($ret === false || !is_array($ret)) return array('ecode' => ERR306);

		if($ret)
		{
			$res = array();
			foreach($ret as $fuid => $frow)
			{       
                                
				$res[$fuid] = $frow;

				$uinfo = daocall("user", "getuser", array($fuid));
				$ext = daocall("user", "getuserext", array($fuid));
				if(!$uinfo || !$ext) continue;

				$res[$fuid] = array(
					"name"	=> $uinfo['name'], 
					"thumb"	=> $uinfo['thumb'], 
					"fuid"	=> intval($fuid), 
					"xp"	=> intval($ext['xp']),
					"oid"	=> $uinfo['oid'],
					"isnew"	=> intval($frow['isnew']),
				);
			}
			unset($ret);
			$ret = &$res;
		}
        
        if (!$ret) return array("ecode" => SUCCESS, 'ret' => array());
        else return array('ecode' => SUCCESS, 'ret' => &$ret);
    }

    public static function richfriends($uid = 0, $page = 1, $pagesize = 500)
	{
        $page = $page ? $page : 1;
        $pagesize = $pagesize ? $pagesize : 500;
        load_sns();
        $sns = new kra(SNS_API_NAME, SNS_API_KEY, SNS_API_SECRET, array("app_id" => SNS_API_ADT_ID, "app_name" => SNS_API_ADT_NAME, "app_key" => SNS_API_ADT_KEY));
        
        $friends = $sns->friends($page, $pagesize, 1, 1);
        if ($friends === false || !is_array($friends)) return array('ecode' => SUCCESS, 'ret' => array());//return array('ecode' => ERR304);
        
        if (!$friends) return array('ecode' => SUCCESS, 'ret' => array());
        
        $fids = daocall("user", "oids2uids", array(array_keys($friends)));
        if ($fids)
		{
            $cfids = array();
			foreach ($friends as $fid => $val)
			{
				$fiuid = intval($fids[$fid]);
				if (!$fiuid)
				{
					unset($friends[$fid]);
					continue;
				}

				$cfids[] = $fiuid;

				$ext = daocall("user", "getuserext", array($fiuid));
				if(!$ext)
				{
					unset($friends[$fid]);
					continue;
				}

				$friends[$fiuid] = array(
					"name"	=> $val['nickname'], 
					"thumb"	=> $val['thumbnail_url'], 
					"fuid"	=> intval($fiuid), 
					"xp"	=> intval($ext['xp']),
					"oid"	=> $fid,
					"isnew"	=> 0,
				);
				unset($friends[$fid]);
			}

            $ufs = daocall("friend", "getfriends", array($uid));
			if($ufs)
			{
				foreach($ufs as $ufsuid => $ufsrow)
				{
					if(!$ufsuid || !$ufsrow || !$friends[$ufsuid])
					{
						unset($ufs[$ufsuid]);
						continue;
					}
					$friends[$ufsuid]['isnew'] = intval($ufsrow['isnew']);
				}
			}
            $ufs = $ufs ? array_keys($ufs) : array();
			$delfd = array();
            if ($ufs)
			{
				$ufd = array_diff($cfids, $ufs);
				$delfd = array_diff($ufs, $cfids);
			}
            else $ufd = &$cfids;
            
            if ($ufd)
			{
                $ufs = array_merge($ufs, $ufd);
                $c = count($ufs);
                $arr = array();
				foreach($ufs as $ufsuid)
				{
					if(!$friends[$ufsuid]) continue;
					$arr[$ufsuid]['isnew'] = 1;
				}
                daocall("friend", "newfriends", array($uid, $arr));
            }

			if($delfd) daocall("friend", "delfriends", array($uid, $delfd));
        }
        return array('ecode' => SUCCESS, 'ret' => &$friends);
    }

	public static function osfriends($uid = 0, $friends = array())
	{
		if(!$uid || !$friends) return array('ecode' => ERR302);

		$fids = daocall("user", "oids2uids", array(array_keys($friends)));
        if ($fids)
		{
            $cfids = array();
			foreach ($friends as $fid => $val)
			{
				$fiuid = intval($fids[$fid]);
				if (!$fiuid)
				{
					unset($friends[$fid]);
					continue;
				}

				$cfids[] = $fiuid;

				$ext = daocall("user", "getuserext", array($fiuid));
				if(!$ext)
				{
					unset($friends[$fid]);
					continue;
				}

				$friends[$fiuid] = array(
					"name"	=> $val['nickname'], 
					"thumb"	=> $val['thumbnail_url'], 
					"fuid"	=> intval($fiuid), 
					"xp"	=> intval($ext['xp']),
					"oid"	=> $fid,
					"isnew"	=> 0,
				);
				unset($friends[$fid]);
			}

            $ufs = daocall("friend", "getfriends", array($uid));
			if($ufs)
			{
				foreach($ufs as $ufsuid => $ufsrow)
				{
					if(!$ufsuid || !$ufsrow || !$friends[$ufsuid])
					{
						unset($ufs[$ufsuid]);
						continue;
					}
					$friends[$ufsuid]['isnew'] = intval($ufsrow['isnew']);
				}
			}
            $ufs = $ufs ? array_keys($ufs) : array();
			$delfd = array();
            if ($ufs)
			{
				$ufd = array_diff($cfids, $ufs);
				$delfd = array_diff($ufs, $cfids);
			}
            else $ufd = &$cfids;
            
            if ($ufd)
			{
                $ufs = array_merge($ufs, $ufd);
                $c = count($ufs);
                $arr = array();
				foreach($ufs as $ufsuid)
				{
					if(!$friends[$ufsuid]) continue;
					$arr[$ufsuid]['isnew'] = 1;
				}
                daocall("friend", "newfriends", array($uid, $arr));
            }

			if($delfd) daocall("friend", "delfriends", array($uid, $delfd));
        }
        return array('ecode' => SUCCESS, 'ret' => &$friends);
	}

    /**
     * 进行第一次访问好友奖励
     * @param type $uid
     * @param type $fuid
     * @return type 
     */
    public function getisnew($uid = 0, $fuid = 0)
	{
		$uid = intval($uid);
        $fuid = intval($fuid);
        if (!$uid || !$fuid) return array('ecode' => ERR302);
        if ($uid == $fuid) return array('ecode' => ERR704);
        
		$flist = daocall("friend", "getfriends", $uid);
        if ($flist === false || !is_array($flist))  return array('ecode' => ERR306);
		if(!isset($flist[$fuid])) return array("ecode" => ERR704);//不是好友

		if($flist[$fuid]['isnew'])
		{
			$addgb = $addxp = 10;
			$addmb = 0;
			$flist[$fuid]['isnew'] = 1;

			$ret = daocall("friend", "setfriend", array($uid, $fuid, array('isnew' => 1)));
			
			if (!$ret) return array('ecode' => ERR705);
			
			//用户扩展数据
			$userext = daocall("user", "getuserext", array($uid));
			if (!$userext)
			{
				//次数减了，没给钱
				self::setcmslog(1, array("uid" => $uid, "xp" => $addxp, "gb" => $addgb, "mb" => $addmb), "friend->getison({$uid}, {$fuid}) 首次访问好友");
				return array("ecode" => ERR402);
			}
			
			$userext['gb'] += $addgb;
			$userext['xp'] += $addxp;

			$rs = array();
			if($addgb) $rs[1] = $addgb;
			if($addxp) $rs[2] = $addxp;

			$ret = daocall("user", "setuserext", array($uid, array(
				'xp' => $userext['xp'], 
				'gb' => $userext['gb'], 
			)));
			if (!$ret)
			{
				//次数减了，没给钱
				self::setcmslog(1, array("uid" => $uid, "xp" => $addxp, "gb" => $addgb, "mb" => $addmb), "friend->getison({$uid}, {$fuid}) 首次访问好友");
				return array('ecode' => ERR407);
			}

			if($addmb) daocall("user", "logmb", array($uid, "获得 {$addmb}MB，friend->getison({$uid}, {$fuid}) 首次访问好友"));

			return array('ecode' => SUCCESS, 'ret' => array("rs" => $rs));
		}
		else{
			return array("ecode" => ERR705);
		}
    }

}

